#include "InputField.h"

#define CLEAR_BUTTON_SIZE 40
#define LABEL_RIGHT_PADDING 10

InputField::InputField()
{
}

InputField::~InputField()
{
}

bool InputField::init(CCRect frame, char* title, int fontSize, float labelWidth){
	setContentSize(frame.size);

	label = CCLabelTTF::create(title, "", fontSize);
	label->setPosition(ccp(labelWidth/2, frame.size.height/2));
	label->setDimensions(CCSizeMake(labelWidth, frame.size.height));
	label->setVerticalAlignment(kCCVerticalTextAlignmentCenter);
	label->setHorizontalAlignment(kCCTextAlignmentRight);
	addChild(label);

	float editBoxWidth = frame.size.width - labelWidth - LABEL_RIGHT_PADDING;
	editBox = CCEditBox::create(CCSizeMake(editBoxWidth, frame.size.height), CCScale9Sprite::createWithSpriteFrameName("TextBoxBackground.png"));
	editBox->setPosition(ccp(frame.size.width/2 + labelWidth/2 + LABEL_RIGHT_PADDING/2, frame.size.height/2));
	editBox->setFontSize(fontSize);
	editBox->setFontColor(ccc3(1, 0, 0));
	addChild(editBox);

	clearButton = CCControlButton::create(std::string("X"), "", 15);
	clearButton->setPosition(ccp(frame.size.width + CLEAR_BUTTON_SIZE/2, frame.size.height/2));
	clearButton->addTargetWithActionForControlEvents(this, cccontrol_selector(InputField::clearButtonTouched), CCControlEventTouchUpInside);
	addChild(clearButton, 1);

	setAnchorPoint(ccp(0.5, 0.5));
	setPosition(ccp(frame.origin.x, frame.origin.y));

	return true;
}

void InputField::clearButtonTouched(CCObject *sender, CCControlEvent controlEvent){
	editBox->setText("");
}